<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物品编码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入物品编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否起" prop="enableFlag">
        <el-input
          v-model="queryParams.enableFlag"
          placeholder="请输入是否起"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['mes:bom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:bom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:bom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['mes:bom:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物品编码" align="center" prop="goodsCode" />
      <el-table-column label="是否起" align="center" prop="enableFlag" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:bom:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:bom:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产物品对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="bomRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="是否起" prop="enableFlag">
          <el-input v-model="form.enableFlag" placeholder="请输入是否起" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">物品物料清单明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddMesBomLine">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteMesBomLine">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="mesBomLineList" :row-class-name="rowMesBomLineIndex" @selection-change="handleMesBomLineSelectionChange" ref="mesBomLine">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物品子码" prop="subGoodsCode" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.subGoodsCode" placeholder="请输入物品子码" />
            </template>
          </el-table-column>
          <el-table-column label="用量" prop="quantity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.quantity" placeholder="请输入用量" />
            </template>
          </el-table-column>
          <el-table-column label="是否起" prop="enableFlag" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.enableFlag" placeholder="请输入是否起" />
            </template>
          </el-table-column>
          <el-table-column label="创建者" prop="createBy" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.createBy" placeholder="请输入创建者" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createTime" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.createTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择创建时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="更新者" prop="updateBy" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.updateBy" placeholder="请输入更新者" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updateTime" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.updateTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择更新时间">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Bom">
import { listBom, getBom, delBom, addBom, updateBom } from "@/api/mes/bom";

const { proxy } = getCurrentInstance();

const bomList = ref([]);
const mesBomLineList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedMesBomLine = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeUpdateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    goodsCode: null,
    enableFlag: null,
  },
  rules: {
    goodsCode: [
      { required: true, message: "物品编码不能为空", trigger: "blur" }
    ],
    enableFlag: [
      { required: true, message: "是否起不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询生产物品列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listBom(queryParams.value).then(response => {
    bomList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    goodsCode: null,
    enableFlag: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  mesBomLineList.value = [];
  proxy.resetForm("bomRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.goodsCode);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加生产物品";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _goodsCode = row.goodsCode || ids.value
  getBom(_goodsCode).then(response => {
    form.value = response.data;
    mesBomLineList.value = response.data.mesBomLineList;
    open.value = true;
    title.value = "修改生产物品";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["bomRef"].validate(valid => {
    if (valid) {
      form.value.mesBomLineList = mesBomLineList.value;
      if (form.value.goodsCode != null) {
        updateBom(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBom(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _goodsCodes = row.goodsCode || ids.value;
  proxy.$modal.confirm('是否确认删除生产物品编号为"' + _goodsCodes + '"的数据项？').then(function() {
    return delBom(_goodsCodes);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 物品物料清单明细序号 */
function rowMesBomLineIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 物品物料清单明细添加按钮操作 */
function handleAddMesBomLine() {
  let obj = {};
  obj.subGoodsCode = "";
  obj.quantity = "";
  obj.enableFlag = "";
  obj.remark = "";
  obj.createBy = "";
  obj.createTime = "";
  obj.updateBy = "";
  obj.updateTime = "";
  mesBomLineList.value.push(obj);
}

/** 物品物料清单明细删除按钮操作 */
function handleDeleteMesBomLine() {
  if (checkedMesBomLine.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的物品物料清单明细数据");
  } else {
    const mesBomLines = mesBomLineList.value;
    const checkedMesBomLines = checkedMesBomLine.value;
    mesBomLineList.value = mesBomLines.filter(function(item) {
      return checkedMesBomLines.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleMesBomLineSelectionChange(selection) {
  checkedMesBomLine.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('mes/bom/export', {
    ...queryParams.value
  }, `bom_${new Date().getTime()}.xlsx`)
}

getList();
</script>
